#include<iostream>
using namespace std;
const int N = 3e4 + 10;
int t, x, y;
int fa[N], d[N], cont[N];
char c;
int n = 3e4;

int find(int a)
{
	if (fa[a] == a)return a;
	int t = find(fa[a]);
	d[a] += d[fa[a]];
	return fa[a] = t;
}
void un(int a, int b)
{
	int Fa = find(a), Fb = find(b);
	if (Fa == Fb)return;
	fa[Fa] = Fb;
	d[Fa] = cont[Fb];
	cont[Fb] += cont[Fa];
}
int qurey(int a, int b)
{

	if (find(a) != find(b))return -1;
	else return abs(d[a] - d[b]) - 1;
}

int main()
{
	cin >> t;
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
		cont[i] = 1;
	}
	while (t--)
	{
		cin >> c >> x >> y;
		if (c == 'M')
		{
			un(x, y);
		}
		else {
			cout << qurey(x, y) << endl;
		}
	}
	return 0;
}